﻿@COFE ?= {}

$ = jQuery
Signin = COFE.Signin
Options = COFE.Options
utils = COFE.utils
Link = COFE.Link
EntryItemList = COFE.EntryItemList
EntryDetailList = COFE.EntryDetailList
HierarchyItemList = COFE.HierarchyItemList
Navigator = COFE.Navigator
Search = COFE.Search

class MobileApp extends Spine.Controller
  constructor : ->
    super     
    @path = Link.fromPath("")    
    @selection = undefined

  elements : 
    "#signin" : "signinPage"  
    "#list" : "entryListPage"
    "#setup" : "setupPage"

  
  init : ->         
    @currentPage = "list"            
  
    $("#list").live('pageinit', () =>  
      
      utils.refresh(
       () => ,
       (err) =>
         @changePage("#signin")
       ) 

      @entryListPage = $("#list")      
      $("#list .header  .headerCaption").bind("click", (event) =>
        # @changePage($("#hierarchy"), "slidedown")        
        utils.showMessage(@path.param + " (" + @path.rel + ")", @path.header)
        )

      @entryList = EntryItemList.init({ el : @entryListPage, childElement : "<li>", App : @ })
      @navigator = Navigator.init({ el : @entryListPage, App : @ })

      @trigger("pathChanged", "Application", @path, true)
    )

    $("#signin").live('pageinit', () =>      
      @signinPage = $("#signin")
      @signin = Signin.init({ el : @signinPage, App : @ })
            
      @bind("signedIn", =>
         @path = Link.fromPath("")
         @changePage("#list") 
         @changePath()
      )      

      @bind("signedOut", =>      
        @changePage("#signin") )      
    )


    $("#details").live('pageinit', () =>        
      @entryDetailsPage = $("#details")      
      $("#details .header  .headerCaption").bind("click", (event) =>
        @changePage($("#hierarchy"), "slidedown")
        )

      @metaList = EntryDetailList.init({ el : @entryDetailsPage, App : @ })              
    )    

    $("#search").live('pageinit', () =>
        @searchPage = $("#search") 
        @search = Search.init({ el : @searchPage, App : @ })  
    )

    $("#hierarchy").live('pageinit', () =>  
      
      @hierarchyListPage = $("#hierarchy")              
      
      $("#hierarchy .header  .headerCaption").bind("click", (event) =>
        @changePage($("#list"), "slideup")
        )
          
      @hierarchyList = HierarchyItemList.init({ el : @hierarchyListPage, App : @ })                   
    )


    $("#hierarchy").live('pagebeforeshow', () => 
      #console.log @path
      @hierarchyList.fetch()
    )

    $("#options").live('pageinit', () =>      
      @optionsPage = $("#options")
      @options = Options.init({ el : @optionsPage, App : @ })
    )    
    
   
  
  parsePathFromHash : () ->
    hash = window.location.hash.slice(1)
    pathRegex = new RegExp("&path=([^&]*)", "i")
    match = hash.match(pathRegex)
    path = if match isnt null then match[1] else ""

  # Inform all class that subscribe to pathChanged event to change to the specified path.
  # params - pathType = (url, folder, search)
  #          path = actual path (e.g. /xyz/abc (url), {temp}\obj (folder), root:{temp}\obj (search))
  #          sender = sender of the request. (Application if not defined.)
  #          isForward = this is for navigation (true or false).
  changePath : (path, sender, isForward) =>        
    @selection = undefined    
    sender ?= "Application"           
    
    if typeof path is "string" then @path = Link.fromPath(path)
    else if path is undefined then @path = Link.fromPath("")

    else if path?.constructor?.name is "Link" then @path = path    
    else throw "Incorrect input " + path

    @trigger("pathChanged", sender, @path, isForward)  

  changeSelection : (entry, sender) =>
    @selection = entry
    if (@selection isnt undefined)
      @changePage($("#details"), "fade")
    @trigger("selectionChanged", sender, @selection)  

  changePage : (page, transition, reverse) =>
    @currentPage = page      
    transition ?= "slide"
    reverse ?= false

    $.mobile.changePage(@currentPage, 
      { 
       transition: transition
       reverse: reverse              
      })

COFE.MobileApp = MobileApp 



  
